package cn.iocoder.yudao.module.product.brand.service;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.thirdparty.zxhx.ZXHXApiClientFactory;
import cn.iocoder.yudao.framework.thirdparty.zxhx.dto.ZXHXBrandPageRespDTO;
import cn.iocoder.yudao.module.product.brand.dal.dataobject.ProductBrandDO;
import cn.iocoder.yudao.module.product.brand.dal.mysql.ProductBrandMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.module.api.supply.ConfigConstants.SUPPLY_NAME_ZXHX;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  这里是文件说明
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/03/04
 */
@Service
@Validated
@Slf4j
public class ZXHXProductBrandSyncServiceImpl implements ZXHXProductBrandSyncService {
    @Resource
    private ProductBrandMapper productBrandMapper;

    private int resultCount = 0;

    @Override
    public int autoSync(int pageNo) throws Exception {
        if (pageNo <= 1) {
            resultCount = 0;
        }
        ZXHXBrandPageRespDTO brandPageRespDTO = ZXHXApiClientFactory.productClient().getBrand(Math.max(1, pageNo), 100);
        List<ZXHXBrandPageRespDTO.DataDTO> data = brandPageRespDTO.getData();
        if (CollUtil.isEmpty(data)) {
            return resultCount;
        }
        List<ProductBrandDO> brandExists = productBrandMapper.selectBySupplyRelatedIds(data.stream().map(ZXHXBrandPageRespDTO.DataDTO::getId).toList());
        List<ProductBrandDO> insertList = new ArrayList<>();
        for (ZXHXBrandPageRespDTO.DataDTO dataDTO : data) {
            if (brandExists.stream().anyMatch(brandDO -> dataDTO.getId().equals(brandDO.getSupplyRelatedId()))) {
                continue;
            }
            ProductBrandDO brandDO = new ProductBrandDO();
            brandDO.setName(dataDTO.getName());
            brandDO.setPicUrl(dataDTO.getThumbnailImg() == null ? "" : dataDTO.getThumbnailImg());
            brandDO.setDescription("");
            brandDO.setSort(dataDTO.getId().intValue());
            brandDO.setStatus(CommonStatusEnum.ENABLE.getStatus());
            brandDO.setSupplyName(SUPPLY_NAME_ZXHX);
            brandDO.setSupplyRelatedId(dataDTO.getId());
            insertList.add(brandDO);

        }
        log.info("插入品牌数据：pageNo={}, {} 条", pageNo, insertList.size());
        productBrandMapper.insertBatch(insertList);
        resultCount += insertList.size();
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            log.warn("插入品牌数据 sleep error: {}", e.getMessage());
        }
        autoSync(pageNo + 1);
        return resultCount;
    }

}
